package com.qumitech.visualnovel.service.skill.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qumitech.visualnovel.component.exception.BusinessException;
import com.qumitech.visualnovel.component.util.ExcelUtils;
import com.qumitech.visualnovel.component.util.FileUtil;
import com.qumitech.visualnovel.component.util.HttpInvokeUtil;
import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.ResultEnum;
import com.qumitech.visualnovel.model.common.UrlConstant;
import com.qumitech.visualnovel.pubdef.dto.skill.*;
import com.qumitech.visualnovel.service.account.AccountService;
import com.qumitech.visualnovel.service.skill.SkillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.*;

/**
 *
 * @author ywxie
 * @date 2020/1/14 10:31
 */
@Service
public class SkillServiceImpl implements SkillService {

    private final String skillAdminUrl = "/skill/admin";

    @Autowired
    private AccountService accountService;

    @Override
    public BaseRsp listSkillConfig() {
        String url = UrlConstant.SERVER_URL + skillAdminUrl + "/config";
        String body = HttpInvokeUtil.httpGet(url, null, null);
        return JSONObject.parseObject(body, BaseRsp.class);
    }

    @Override
    public BaseRsp listSkillVerifyAdmin(Map<String, String> paramMap) {
        String url = UrlConstant.SERVER_URL + skillAdminUrl + "/verify";
        String body = HttpInvokeUtil.httpGet(url, paramMap, null);
        return JSONObject.parseObject(body, BaseRsp.class);
    }

    @Override
    public BaseRsp updateSkillVerifyAdmin(SkillVerifyAdminUpdateDTO updateDTO) {
        updateDTO.setOperator(accountService.getOperatorName());
        String url = UrlConstant.SERVER_URL + skillAdminUrl + "/verify";
        String req = JSONObject.toJSONString(updateDTO);
        String body = HttpInvokeUtil.httpPutJson(url, req, UrlConstant.DefaultEncode, null);
        return JSONObject.parseObject(body, BaseRsp.class);
    }

    @Override
    public BaseRsp listUserSkillAdmin(Map<String, String> paramMap) {
        String url = UrlConstant.SERVER_URL + skillAdminUrl;
        String body = HttpInvokeUtil.httpGet(url, paramMap, null);
        return JSONObject.parseObject(body, BaseRsp.class);
    }

    @Override
    public Map<String, Object> listUserSkillAdminExport(Map<String, String> paramMap, HttpServletResponse response) {
        BaseRsp rsp = listUserSkillAdmin(paramMap);
        Map<String, Object> rspMap = (Map<String, Object>) rsp.getData();
        Map<String, Object> resultMap = new HashMap<>(16);

        try {
            JSONArray skillList = (JSONArray) rspMap.get("skill_admin_list");
            List<SkillAdminExportDTO> skillAdminList = new ArrayList<>(16);
            for (int i = 0; i < skillList.size(); i++) {
                SkillAdminExportDTO exportDTO = new SkillAdminExportDTO();
                exportDTO.setId(skillList.getJSONObject(i).get("id").toString());
                exportDTO.setPriceBase((String) skillList.getJSONObject(i).get("price_base"));
                exportDTO.setUid(skillList.getJSONObject(i).get("uid").toString());
                if (skillList.getJSONObject(i).get("recommend_score") != null) {
                    exportDTO.setRecommendScore(skillList.getJSONObject(i).get("recommend_score").toString());
                }
                exportDTO.setUserName((String) skillList.getJSONObject(i).get("user_name"));
                exportDTO.setOrderCount(skillList.getJSONObject(i).get("order_count").toString());
                exportDTO.setOpenFlag(skillList.getJSONObject(i).get("open_flag").toString());
                exportDTO.setMainFlag(skillList.getJSONObject(i).get("main_flag").toString());
                exportDTO.setSex((String) skillList.getJSONObject(i).get("sex"));
                exportDTO.setLevelName((String) skillList.getJSONObject(i).get("level_name"));
                exportDTO.setNote((String) skillList.getJSONObject(i).get("note"));
                exportDTO.setAcceptOrderCount(skillList.getJSONObject(i).get("accept_order_count").toString());
                exportDTO.setUnacceptOrderCount(skillList.getJSONObject(i).get("unaccept_order_count").toString());
                exportDTO.setOrderCount(skillList.getJSONObject(i).get("order_count").toString());
                exportDTO.setVerifyTime((String) skillList.getJSONObject(i).get("verify_time"));
                exportDTO.setForbidFlag(skillList.getJSONObject(i).get("forbid_flag").toString());
                exportDTO.setForbiddenState(skillList.getJSONObject(i).get("forbidden_state").toString());
                exportDTO.setStar(skillList.getJSONObject(i).get("star").toString());
                exportDTO.setAppraiseCount(skillList.getJSONObject(i).get("appraise_count").toString());
                exportDTO.setSkillId(skillList.getJSONObject(i).get("skill_id").toString());
                exportDTO.setSkillName(skillList.getJSONObject(i).get("skill_name").toString());
                exportDTO.setCategory(skillList.getJSONObject(i).get("category").toString());
                exportDTO.setRevenue(skillList.getJSONObject(i).get("revenue").toString());
                exportDTO.setPrice(skillList.getJSONObject(i).get("price").toString());
                skillAdminList.add(exportDTO);
            }

            ExcelUtils<SkillAdminExportDTO> xls = new ExcelUtils<>(SkillAdminExportDTO.class);
            String filename = FileUtil.getExcelName();
            response.reset();
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + filename);
            OutputStream outputStream = response.getOutputStream();
            xls.exportExcel("大神技能列表", skillAdminList, outputStream);
            outputStream.flush();
            outputStream.close();
            resultMap.put("title", filename);
            return resultMap;
        } catch (IOException e) {
            e.printStackTrace();
        }

        throw new BusinessException(ResultEnum.EXCEL_EXPORT_ERROR);
    }

    @Override
    public BaseRsp listGodAdmin(Map<String, String> paramMap) {
        String url = UrlConstant.SERVER_URL + skillAdminUrl + "/god";
        String body = HttpInvokeUtil.httpGet(url, paramMap, null);
        return JSONObject.parseObject(body, BaseRsp.class);
    }

    @Override
    public BaseRsp updateGodAdmin(SkillGodUpdateAdminDTO dto) {
        String url = UrlConstant.SERVER_URL + skillAdminUrl + "/update";
        String req = JSONObject.toJSONString(dto);
        String body = HttpInvokeUtil.httpPostJson(url, req, UrlConstant.DefaultEncode, null);
        return JSONObject.parseObject(body, BaseRsp.class);
    }

    @Override
    public Map<String, Object> listGodAdminExport(Map<String, String> paramMap, HttpServletResponse response) {
        BaseRsp rsp = listGodAdmin(paramMap);
        Map<String, Object> rspMap = (Map<String, Object>) rsp.getData();
        Map<String, Object> resultMap = new HashMap<>(16);

        try {
            JSONArray godList = (JSONArray) rspMap.get("god_admin_list");
            List<GodAdminExportDTO> godAdminList = new ArrayList<>(16);
            for (int i = 0; i < godList.size(); i++) {
                GodAdminExportDTO exportDTO = new GodAdminExportDTO();
                exportDTO.setUid(godList.getJSONObject(i).get("uid").toString());
                exportDTO.setPresentId((String) godList.getJSONObject(i).get("present_id"));
                exportDTO.setUserName((String) godList.getJSONObject(i).get("user_name"));
                exportDTO.setOrderCount((Integer) godList.getJSONObject(i).get("order_count"));
                exportDTO.setOpenFlag(godList.getJSONObject(i).get("open_flag").toString());
                exportDTO.setSex((String) godList.getJSONObject(i).get("sex"));
                exportDTO.setIdCard((String) godList.getJSONObject(i).get("id_card"));
                exportDTO.setRealName((String) godList.getJSONObject(i).get("real_name"));
                exportDTO.setVerifyTime((String) godList.getJSONObject(i).get("verify_time"));
                exportDTO.setForbidFlag(godList.getJSONObject(i).get("forbid_flag").toString());
                exportDTO.setForbiddenState(godList.getJSONObject(i).get("forbidden_state").toString());
                exportDTO.setLabel((String) godList.getJSONObject(i).get("label"));
                exportDTO.setTopType(godList.getJSONObject(i).get("top_type").toString());
                exportDTO.setPhoneNum((String) godList.getJSONObject(i).get("phone_number"));
                exportDTO.setState(godList.getJSONObject(i).get("state").toString());
                exportDTO.setRevenue(godList.getJSONObject(i).get("revenue").toString());
                godAdminList.add(exportDTO);
            }

            ExcelUtils<GodAdminExportDTO> xls = new ExcelUtils<>(GodAdminExportDTO.class);
            String filename = FileUtil.getExcelName();
            response.reset();
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + filename);
            OutputStream outputStream = response.getOutputStream();
            xls.exportExcel("大神列表", godAdminList, outputStream);
            outputStream.flush();
            outputStream.close();
            resultMap.put("title", filename);
            return resultMap;
        } catch (IOException e) {
            e.printStackTrace();
        }

        throw new BusinessException(ResultEnum.EXCEL_EXPORT_ERROR);
    }

    @Override
    public BaseRsp listSkillForbiddenAdmin(Map<String, String> paramMap) {
        String url = UrlConstant.SERVER_URL + skillAdminUrl + "/forbid";
        String body = HttpInvokeUtil.httpGet(url, paramMap, null);
        return JSONObject.parseObject(body, BaseRsp.class);
    }

    @Override
    public BaseRsp updateSkillForbiddenAdmin(SkillForbiddenAdminUpdateDTO updateDTO) {
        updateDTO.setOperator(accountService.getOperatorName());
        String url = UrlConstant.SERVER_URL + skillAdminUrl + "/forbid";
        String req = JSONObject.toJSONString(updateDTO);
        String body = HttpInvokeUtil.httpPutJson(url, req, UrlConstant.DefaultEncode, null);
        return JSONObject.parseObject(body, BaseRsp.class);
    }

    @Override
    public BaseRsp listGodRecommendAdminByConditions(Map<String, String> paramMap) {
        String url = UrlConstant.SERVER_URL + skillAdminUrl + "/recommend";
        String body = HttpInvokeUtil.httpGet(url, paramMap, null);
        return JSONObject.parseObject(body, BaseRsp.class);
    }

    @Override
    public BaseRsp addGodRecommendAdmin(GodRecommendAdminAddDTO addDTO) {
        addDTO.setOperator(accountService.getOperatorName());
        String url = UrlConstant.SERVER_URL + skillAdminUrl + "/recommend";
        String req = JSONObject.toJSONString(addDTO);
        String body = HttpInvokeUtil.httpPostJson(url, req, UrlConstant.DefaultEncode, null);
        return JSONObject.parseObject(body, BaseRsp.class);
    }

    @Override
    public BaseRsp updateGodRecommendAdmin(GodRecommendAdminUpdateDTO updateDTO) {
        updateDTO.setOperator(accountService.getOperatorName());
        String url = UrlConstant.SERVER_URL + skillAdminUrl + "/recommend";
        String req = JSONObject.toJSONString(updateDTO);
        String body = HttpInvokeUtil.httpPutJson(url, req, UrlConstant.DefaultEncode, null);
        return JSONObject.parseObject(body, BaseRsp.class);
    }

    @Override
    public BaseRsp batchSetSkillPricesAdmin(Long uid, Integer skillId, String prices, Integer orderCount) {
        String url = UrlConstant.SERVER_URL + skillAdminUrl + "/price";
        Map<String, Object> param = new HashMap<>(16);
        param.put("uid", uid);
        param.put("skill_id", skillId);
        param.put("prices", prices);
        param.put("order_count", orderCount);
        String req = JSONObject.toJSONString(param);
        String body = HttpInvokeUtil.httpPutJson(url, req, UrlConstant.DefaultEncode, null);
        return JSONObject.parseObject(body, BaseRsp.class);
    }

    @Override
    public BaseRsp getSkillPriceAdmin(Long uid, Integer skillId) {
        String url = UrlConstant.SERVER_URL + skillAdminUrl + "/price";
        Map<String, String> param = new HashMap<>(16);
        param.put("uid", uid.toString());
        param.put("skill_id", skillId.toString());
        String body = HttpInvokeUtil.httpGet(url, param, null);
        return JSONObject.parseObject(body, BaseRsp.class);
    }
}
